package com.sgzn.sgpay.container;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;

/**
 * 通过服务名称获取服务
 * @author huangsenguang
 * @version Id: ServicePrivinder.java, v 0.1 2018/7/28 23:26 huangsenguang Exp $$
 */
public class ServicePrivinder {
    /**
     * spring bean上下文， 从ServicePrivinderCore获取
     */
    public static ApplicationContext context;

    public static Object getBean(String beanName) {
        if (StringUtils.isBlank(beanName)) {
            throw new RuntimeException("您要访问的服务名称不能为空");
        }
        Object bean = null;
        //容器包含beanName
        if (context.containsBean(beanName)) {
            bean = context.getBean(beanName);
        }

        if (bean == null) {
            throw new RuntimeException("您要访问的服务名称不存在[" + beanName + "]不存在");
        }
        return bean;
    }
}

